﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NetworkAnalyzer
{
    public partial class MysqlConfigForm : Form
    {
        public MysqlConfigForm(Form parent)
        {
            InitializeComponent();
            Owner = parent;
        }

        private void MysqlAutoConfigForm_Load(object sender, EventArgs e)
        {
            textBoxHostname.Text = Properties.Settings.Default.db_hostname;
            textBoxUsername.Text = Properties.Settings.Default.db_username;
            textBoxPassword.Text = Properties.Settings.Default.db_password;
            textBoxDatabase.Text = Properties.Settings.Default.db_database;
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.db_hostname = textBoxHostname.Text;
            Properties.Settings.Default.db_username = textBoxUsername.Text;
            Properties.Settings.Default.db_password = textBoxPassword.Text;
            Properties.Settings.Default.db_database = textBoxDatabase.Text;

            Properties.Settings.Default.Save();

            MessageBox.Show("Data saved", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void buttonTestCon_Click(object sender, EventArgs e)
        {
            var code = SQL.MysqlDatabaseConnection.TestConnection(textBoxHostname.Text, textBoxUsername.Text, textBoxPassword.Text,
                                                       textBoxDatabase.Text);
            if (code == 0)
            {
                MessageBox.Show("Success", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("Failure. (Errno: " + code + ")", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Save();
            Dispose();
        }
    }
}
